package com.skylun.http;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.topchoice.mobile.library.net.http.async.AsyncHttpClient;
import cn.com.topchoice.mobile.library.net.http.async.AsyncHttpResponseHandler;
import cn.com.topchoice.mobile.library.net.http.async.BinaryHttpResponseHandler;
import cn.com.topchoice.mobile.library.net.http.async.JsonHttpResponseHandler;
import cn.com.topchoice.mobile.library.net.img.WebImage;


public class HttpTestActivity extends Activity implements OnClickListener{

	private Button btn;
	private Button btn2;
	private TextView textView;
	private EditText username;
	private Handler handler = null;
	private Thread reqThread =null;
	
	private ImageView imageView = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        btn = (Button)this.findViewById(R.id.btn);
        btn2 = (Button)this.findViewById(R.id.btn2);
        textView = (TextView)this.findViewById(R.id.textView);
        username = (EditText)this.findViewById(R.id.username);
        
        imageView = (ImageView)this.findViewById(R.id.img1);
        
        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);
        
        
        /*handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 0x123) {
					Log.v("skylun", "------------------handler result:"+msg.obj.toString());
					textView.setText(msg.obj.toString());
				}

			}

		};*/
		
        
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn:
				/*Log.v("skylun", "------------------1");
				reqThread = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
						String result = HttpUtil.getRquest(HttpUtil.BASE_URL+"index.php?r=session/index&is_login="+username.getText().toString());
						Log.v("skylun", "------------------result:"+result);
						
						Message msg = new Message();
						msg.what = 0x123;
						msg.obj = result;
						handler.sendMessage(msg);
						} catch (Exception e) {
							Log.v("skylun", "------------------result:"+e.getMessage());
							e.printStackTrace();
						}
					}
				}); 
				reqThread.start();*/
			
			AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
			asyncHttpClient.get(HttpUtil.BASE_URL+"index.php?r=session/index&is_login=1", new AsyncHttpResponseHandler() {
				public void onStart() {
					System.out.println("onStart()");
				}

				public void onFinish() {
					System.out.println("onFinish()");
				}

				public void onSuccess(String content) {
					//System.out.println("onSuccess() content->"+content);
					textView.setText("hi");
				}
				
				public void onFailure(Throwable error, String content) {
					System.out.println("onFailure() error->"+error+" content->"+content);
				}
	       });
			
			asyncHttpClient.get(HttpUtil.BASE_URL+"index.php?r=json/index", new JsonHttpResponseHandler() {
				public void onStart() {
					System.out.println("onStart()");
				}

				public void onFinish() {
					System.out.println("onFinish()");
				}

				public void onSuccess(JSONObject response) {
					try {
						System.out.println("onSuccess() JSONObject response->"+response.getString("error"));
					} catch (JSONException e) {
						e.printStackTrace();
						
					}
				}

				public void onSuccess(JSONArray response) {
					System.out.println("onSuccess() JSONArray");
				}
				
				public void onFailure(Throwable error, String content) {
					System.out.println("onFailure() error->"+error+" content->"+content);
				}
	       });
			
			  String[] allowedTypes = new String[] { "image/jpeg" };
			  asyncHttpClient.get("http://dzl.baidu.com/iebar/images/dzl_20120627_1312.jpg", new BinaryHttpResponseHandler(allowedTypes) {
			      public void onSuccess(byte[] imageData) {
			          // Successfully got a response
			    	  InputStream in = new ByteArrayInputStream(imageData);
			    	  Bitmap bitmap = BitmapFactory.decodeStream(in);
			    	  imageView.setImageBitmap(bitmap);
			    	  System.out.println("onSuccess() imageData len->"+imageData.length);
			      }
			 
			      public void onFailure(Throwable e, byte[] imageData) {
			          // Response failed 
			    	  System.out.println("onFailure() imageData error->"+e.getMessage());
			      }
			  });
			
			
			
		    String url = "http://www.2cto.com/uploadfile/2011/1205/thumb_126_90_20111205045331102.jpg";
			WebImage webImage = new WebImage(url);
			Bitmap bitMap =  webImage.getBitmap(this);
			
			//imageView.setImageBitmap(bitMap);
			
				
			break;
		case R.id.btn2:
			try {
				String result = HttpUtil.getRquest(HttpUtil.BASE_URL+"index.php?r=session/index");
				textView.setText(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		}
		
	}

}

